package org.flyframework.dialog
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	
	public class AlertBuilder
	{
		
		private var _text:String;
		private var _title:String;
		private var iconClass:Class;
		
		private var _parent:Sprite;
		private var _flags:uint = 0;
		private var defaultButtonFlag:uint = Alert.OK;
		
		private var closeHandler:Function = defaultCloseHandler;
		private var okHandler:Function;
		private var yesHandler:Function;
		private var noHandler:Function;
		
		
		//--------------------------------------------------------------------------
		// Init
		//--------------------------------------------------------------------------
		
		public function AlertBuilder( text:String=null, title:String=null )
		{
			this._text = text;
			this._title = title;
		}
		
		
		//--------------------------------------------------------------------------
		// Build
		//--------------------------------------------------------------------------
		
		public function text( text:String ):AlertBuilder
		{
			this._text = text;
			return this;
		}
		
		public function title( title:String ):AlertBuilder
		{
			this._title = title;
			return this;
		}
		
		public function icon( iconClass:Class ):AlertBuilder
		{
			this.iconClass = iconClass;
			return this;
		}
		
		public function parent( parent:Sprite ):AlertBuilder
		{
			this._parent = parent;
			return this;
		}
		
		public function close( closeHandler:Function ):AlertBuilder
		{
			this.closeHandler = closeHandler;
			return this;
		}
		
		public function modal( modal:Boolean ):AlertBuilder
		{
			if( modal == false )
			{
				this._flags = this._flags | Alert.NONMODAL;	
			}
			return this;
		}
		
		
		public function buttons( buttonFlags:uint, defaultButtonFlag:uint=0 ):AlertBuilder
		{
			this._flags = this._flags | buttonFlags;
			
			if( defaultButtonFlag > 0 )
			{
				this.defaultButtonFlag = defaultButtonFlag;
			}
			
			return this;
		}
		
		public function ok( okHandler:Function=null, isDefault:Boolean=false ):AlertBuilder
		{
			flagButton( Alert.OK, isDefault );
			this.okHandler = okHandler;
			return this;
		}
		
		public function yes( yesHandler:Function=null, isDefault:Boolean=false ):AlertBuilder
		{
			flagButton( Alert.YES, isDefault );
			this.yesHandler = yesHandler;
			return this;
		}
		
		public function no( noHandler:Function=null, isDefault:Boolean=false ):AlertBuilder
		{
			flagButton( Alert.NO, isDefault );
			this.noHandler = noHandler;
			return this;
		}
		
		
		protected function flagButton( buttonFlag:uint, isDefault:Boolean ):void
		{
			this._flags = this._flags | buttonFlag;
			
			if( isDefault == true )
			{
				this.defaultButtonFlag = buttonFlag;
			}
		}
		
		
		//--------------------------------------------------------------------------
		// 
		//--------------------------------------------------------------------------
		
		public function show( event:Event=null ):void
		{
			Alert.show( this._text, this._title, this._flags, this._parent, 
				this.closeHandler, this.iconClass, this.defaultButtonFlag );
		}
		
		public function asFunction():Function
		{
			var showFunction:Function = function( event:Event=null ):void
			{
				show( event );
			};
			return showFunction;
		}
		
		protected function defaultCloseHandler( event:CloseEvent ):void
		{
			if( event.detail == Alert.OK && this.okHandler != null )
			{
				this.okHandler( event );
			}
			else if( event.detail == Alert.YES && this.yesHandler != null )
			{
				this.yesHandler( event );
			}
			else if( event.detail == Alert.NO && this.noHandler != null )
			{
				this.noHandler( event );
			}
		}
		
	}
}